﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Services.Interface;
using HanGu.Entity;
using HanGu.Common;

namespace Hangu.WebClient.Controllers.Admin
{
    public class ModuleController : AdminBaseController
    {
        private static readonly IModule serviceModule = ServiceFactory.GetService<IModule>();

        //
        // GET: /Module/

        public ActionResult Index()
        {
            ViewData["ModuleList"] = GetDropModuleList();
            return View(viewList, GetModuleList());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            int parentID = 0;
            int.TryParse(Request["IntObj1"], out parentID);
            SearchParma.IntObj1 = parentID;
            ViewData["ModuleList"] = GetDropModuleList();
            SearchParma.Pagination.RowCount = null;
            return View(viewList, GetModuleList());
        }

        List<Module> GetModuleList()
        {
            List<Module> moduleList = serviceModule.GetAll(base.SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return moduleList;
        }
        SelectList GetDropModuleList()
        {
            var list = serviceModule.GetAll();
            list.Add(new Module() { Title = "--------请选择--------" });
            SelectList selectList = new SelectList(list, "ID", "Title");
            return selectList;

        }
        public ActionResult Create(int id)
        {
            Module module = new Module() { GUID = Helper.GetGuid };
            if (id > 0)
            {
                module = serviceModule.GetByID(id);
            }
            SearchParma.IntObj1 = id;
            ViewData["ModuleList"] = GetDropModuleList();
            return View(viewCreate, module);
        }

        //
        // POST: /Module/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Module Module = new Module();
                TryUpdateModel(Module, collection);
                if (Module.ID > 0)
                {
                    serviceModule.Update(Module);
                }
                else
                {
                    serviceModule.Add(Module);
                }
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }

        //
        // GET: /Module/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Module/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Module/Delete/5
        [HttpPost]
        public string Delete(int id, string guid)
        {
            if (id < 10)
            {
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SystemDelFail, navTabId, "", "");
            }
            else
            {
                try
                {
                    serviceModule.Delete(id);
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                }
                catch (Exception ex)
                {
                    ExceptionHand(ex);
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
                }
            }

            return "{" + ajaxStr + "}";

        }
    }
}
